<?php
/**
 * @version		$Id: mod_M17n.php 8152 2008-03-01 08:00:00Z jj $
 * @package		Joomla M17n
 * @copyright	Copyright (C) 2005 - 2007 SDIC SA
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
 **/

/**
 * CHANGELOG:
 * 1.5.1.b
 * March 12, Added submission from Tobias Frash for Legacy Fix
 * March 12, Corrected Manual ItemId implementation
 * 1.5.2
 * April 15, Fixed 1.5.2 menu administration bug (Javascript ID)
 * April 15, Added auto-dection of language from url itemid
 * 
 **/

	// no direct access
	defined('_JEXEC') or die('Restricted access');
	
	// Retrieve Variables & Parameters
	$template=$mainframe->getTemplate();
	global $nowLang, $langList, $homeList;
	
	$lsprefix=$params->get('langprefix');
	$lssuffix=$params->get('langsuffix');
	$showmode=$params->get('displaysas');
	$iconpath=$params->get('iconfolder');
	$actvlang=$params->get('activelang');
	$hilitclr=$params->get('hilitcolor');
	$separate=$params->get('langspacer');
	$custspce=$params->get('custspacer');
	$spcestyl=$params->get('spacestyle');
	$remember=$params->get('rememberin');
	$custlaps=$params->get('definedrem');
	
	if($actvlang=='hidden'){
		foreach($langList as $k=>$lang){
			if($lang['value']==$nowLang){
				unset($langList[$k]);
				break;
			}
		}
	}
	
	$setLang=JRequest::getVar('setLang','');
	$url=JRequest::getURI();
	$user=JFactory::getUser();
	
	// Clears Cookie if Set to session lifetime
	if($remember=='session' && isset($_COOKIE['saveLang']) && $_COOKIE['saveLang']!=''){
		setCookie('saveLang','');
		unset($_COOKIE['saveLang']);
	}
	
	// Detects Cookie Language if it exists
	$forceSet=false;
	if($user->getParam('language')=='' && $setLang==""){
		if(isset($_COOKIE['saveLang'])){
			$forceSet=true;
			$setLang=$_COOKIE['saveLang'];
		}
	}
	
	// Changes language if new value is defined
	if($setLang!=""){
		// Remove Itemid from URL and clean it
		$itemid=$_REQUEST['Itemid'];
		$quoPos=strPos($url,'?');
		$nowPage=substr($url,0,$quoPos);
		
		/*
		$idPos=strPos($url,'Itemid=');
		$removeId='';
		if($idPos!==false){
			$idEnd=strPos($url,'&');
			if($idEnd!==false){
				$removeId=subStr($url,$idPos,$idEnd-$idPos+1);
			}else{
				$idEnd=strLen($url);
				$removeId=subStr($url,$idPos,$idEnd-$idPos);
			}
		}
		if($removeId!='')$url=str_replace($removeId,'',$url);
		$lastChar=subStr($url,-1);
		if($lastChar=='?')$url=subStr($url,0,-1);
		*/
		
		// Set User Session and Cookie if applicable
		$user->setParam( 'language', $setLang );
		
		if($remember=='forever'){
			$century=mktime()+(3600*24*365);
			setCookie('saveLang',$setLang,$century,'/');
		}
		if($remember=='defined'){
			if(!$custlaps || $custlaps<=0)$custlaps=1;
			$period=mktime()+(3600*24*$custlaps);
			setCookie('saveLang',$setLang,$period,'/');
		}
		
		// Redirect User
		$nURL=$nowPage.'?Itemid='.$itemid;
		$nURL=JRoute::_($nURL);
		$mainframe->redirect($nURL);
		$mainframe->close();
	}
	
	
	
	
	// Prepares output (Language Selector)
	$style='';
	$setStyle='';
	
	if($spcestyl!='')$setStyle.=$spcestyl;
	
		
	$out=Array();
	foreach($langList as $lang){
		
		// Define if current lang is $nowLang
		$currentLang=false;
		if($lang['value']==$nowLang)$currentLang=true;
		
		// Apply Hilight Color if required
		$addStyle='';
		if(($actvlang=='backgr' || $actvlang=='border') && $currentLang){
			if($setStyle!='')$addStyle.=';';
			if($actvlang=='backgr')$addStyle.='background-color:'.$hilitclr;
			if($actvlang=='border')$addStyle.='border: solid '.$hilitclr.' 1px';
		}
			
		// Define Style
		if($setStyle!='' || $addStyle!=''){
			$style=' style="'.$setStyle.$addStyle.'"';
		}else{
			$style='';
		}


		
		// Append each language to output
	//	$itemid=$homeList[$lang['value']];
	###	Submitted by Tobias Frasch & adapted, thanks Tobias
		$itemid = '';
		$link = '';
		if(isset($homeList[$lang['value']]) && !empty($homeList[$lang['value']])){	
			$itemid = $homeList[$lang['value']]['item'];
			$link = $homeList[$lang['value']]['link'];
		}
	##>
		
		// Skip current language if itemid is empty
		if($itemid=='')continue;
		
		// Added QuestMark Detection
		if(strrpos($link,'?')!==false)
			$link.='&';
		else
			$link.='?';
		
	//	$link='index.php?'.'setLang='.$lang['value'].'&Itemid='.$itemid;
	###	Submitted by Tobias Frasch & Adapted
		$link.='setLang='.$lang['value'];
	### Submitted by Nick Davina
		if( stristr($link,'Itemid=')===false )$link.='&Itemid='.$itemid;
		
		//$img='<img src="'.$imgPath.$lang['value'].'.gif" alt="'.$lang['text'].'" title="'.$lang['text'].'" border="0">';
		$subtag=split('-',$lang['value']);
		$subtag=$subtag[0];
		
		// Append language according to showmode (displaysas param)
		if($showmode=='icon'){
			$subtag=strToLower($subtag);
			$imgPath=$iconpath;
			if(subStr($imgPath,-1)!='/')$imgPath.='/';
			$img='<img src="'.$imgPath.$subtag.'.gif" alt="'.$lang['text'].'" title="'.$lang['text'].'" border="0"'.$style.' />';
			$langLabel=$img;
		}elseif($showmode=='code'){
			$subtag=strToUpper($subtag);
			$langLabel='<span'.$style.'>'.$subtag.'</span>';
		}elseif($showmode=='shrt'){
			$label=$lang['text'];
			$brack=strPos($label,'(');
			$label=subStr($label,0,$brack);
			$langLabel='<span'.$style.'>'.$label.'</span>';
		}else{
			$langLabel='<span'.$style.'>'.$lang['text'].'</span>';
		}
		
		if($actvlang=='nolink' && $currentLang){
			$out[]=$langLabel;
		}else{
			$out[]='<a href="'.$link.'">'.$langLabel.'</a>';	
		}
	}
	
	// Rendering, defines the language separator
	if($separate=='spacer'){
		$spacer="&nbsp;";
	}elseif($separate=='widesp'){
		$spacer=" &nbsp; ";
	}elseif($separate=='defined'){
		$spacer=$custspce;
	}else{
		$spacer="<br />";
	}
	
	// Rendering, displays the language selector
	$outPut="";
	if($lsprefix!='')$outPut=$lsprefix.$spacer;
	$outPut.=join($spacer,$out);
	if($lssuffix!='')$outPut.=$spacer.$lssuffix;
	
	echo $outPut;
?>